package com.gitee.wsl.logger.format

import com.gitee.wsl.logger.LogFormatter
import com.gitee.wsl.logger.LogLevel
import com.gitee.wsl.logger.LogPrinter
import com.gitee.wsl.logger.format

class JsonLogFormatter(val options: JsonLogFormatterOptions = JsonLogFormatterOptions()):LogFormatter {
    override fun format(
        level: LogLevel,
        tag: String,
        messageAny: Any?,
        throwable: Throwable?
    ): String = buildMap {
        put("level", level.name)
        put("message", messageAny)
//        if (options.source) {
//            put("source", log.source)
//        }
//        val status = log.status
//        if (status != null && options.status) {
//            put("status", status.name)
//        }
//        if (options.verbose) {
//            val map = log.metadata.toMutableMap()
//            putAll(map)
//        }
    }.entries.joinToString(prefix = "{${options.tab}\n", separator = ",\n", postfix = "\n}") { (key, value) ->
        """${options.tab}"$key": "$value""""
    }

}

fun LogPrinter.useJsonFormat(options: JsonLogFormatterOptions = JsonLogFormatterOptions()) = format(JsonLogFormatter(options))

class JsonLogFormatterOptions(
    val tab: String = "  ",
    val verbose: Boolean = true,
    val status: Boolean = true,
    val source: Boolean = false
)